-- monster_clear
-- Created by chengb Nov/11/2015
-- 技能：清除所有怪物(对boss无效)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 计算触发概率
        local base = para[1];
        local rate = 0;
        if #para > 0 and type(para[2]) == "number" then
            rate = para[2];
            local level = extra["level"] or 0;
            if level > 0 then
                rate = base + rate * level;
            end
        end

        -- 判断是否触发清怪
        local rand = DungeonM.getRandSeed("monster_clear") % 1000;
        if rand >= rate then
            return;
        end

        -- 遍历所有的怪
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local grid = DungeonM.getGridByPos(p);

            -- 是未死亡的怪物
            if grid:isOpened() and
               grid:isMonster() and
               not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        for _, monster in pairs(ret) do
            -- 击杀所有小怪
            if not monster:isBoss() then
                local finalDamage = monster:getHp();

                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end

                SkillM.getSequence(monster):hit(source, monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, monster, finalDamage, skillId, extra);
            end
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];
        local rate = para[2];
        if SlimeSkillM.isSlimeSkill(skillId) then
            -- 冈布奥主动技
            local id = SlimeSkillM.getSlimeActiveSkillId(skillId);
            local level = SlimeSkillM.getSkillLevel(id);
            rate = base + level * rate;
        end

        rate = rate / 10;
        desc = string.gsub(desc, "{monster_clear_rate}", rate);

        return desc;
    end,
};
